package com.weipu.common.facade.support.service.task;

import java.io.File;
import java.util.ArrayList;

import android.graphics.Bitmap;


import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;
import com.weipu.common.DXApplication;
import com.weipu.common.facade.model.cache.ImageDownloadParam;
//import com.weipu.common.image.ImageCache;
//import com.weipu.common.image.ImageFetcher;
//import com.weipu.common.image.ImageResizer;
//import com.weipu.common.image.Utils;

/**
 * 预加载缓存图片任务，该任务会下载图片并缓存本地，UI通过缓存ImageWorker读取缓存数据
 * 对于一些图片资源预先加载以便程序再次访问直接通过ImageWorker缓存返回
 */
public class PreLoadCacheImageTask extends BaseServiceTask<ArrayList<ImageDownloadParam>>
{
    
    /**
     * 
     */
    private static final long serialVersionUID = 345951230620836929L;
    
    private ArrayList<ImageDownloadParam> imageUrlList;//图片地址
    
    public PreLoadCacheImageTask(ArrayList<ImageDownloadParam> imageUrlList)
    {
        this.imageUrlList = imageUrlList;
    }
    
    public PreLoadCacheImageTask(ImageDownloadParam imageUrl)
    {
        imageUrlList = new ArrayList<ImageDownloadParam>(1);
        imageUrlList.add(imageUrl);
    }
    
    @Override
    protected boolean executeRequest()
    {
        return true;
    }
    
    //任务执行概要： 产品链表任务、目录链表任务(依赖于产品链表最后一个任务完成)    
    @Override
    protected ArrayList<ImageDownloadParam> execute()
    {
//        if (null == imageUrlList || imageUrlList.isEmpty())
//        {
//            return null;
//        }
//        
//        //                //属于立即执行的任务则启用多线程执行下载任务
//        //                if (isExecuteImmediate())
//        //                {
//        //                    
//        //                }
//        
//        //ImageCache cache = new ImageCache(DXApplication.getContext(), ImageFetcher.IMAGE_CACHE_DIR);
//        ImageCache.ImageCacheParams cacheParams = new ImageCache.ImageCacheParams(ImageFetcher.IMAGE_CACHE_DIR);
//        cacheParams.memCacheSize = 1024 * 1024 * Utils.getMemoryClass(DXApplication.getContext()) / 7;
//        ImageCache imageCache = ImageCache.findOrCreateCache(DXApplication.getContext(), cacheParams);
//        for (int i = 0; i < imageUrlList.size(); i++)
//        {
//            cacheImage(imageCache, imageUrlList.get(i));
//        }
//        
    	 for (int i = 0; i < imageUrlList.size(); i++)
         {
    	     ImageLoader.getInstance().loadImage(imageUrlList.get(i).getImageUrl(), new SimpleImageLoadingListener());  
         }
    	return imageUrlList;
    }
    
//    //缓存下载图片
//    private void cacheImage(ImageCache cache, ImageDownloadParam imageUrl)
//    {
//        File tmpFile = null;
//        
//        Bitmap bitmap = null;
//        
////        //下载文件
////        tmpFile = ImageFetcher.downloadBitmap(DXApplication.getContext(), imageUrl.getImageUrl());
////        
////        if (null == tmpFile)
////        {
////            return;
////        }
////        
////        //文件转BitMap并压缩
////        //FIXME 尺寸暂时为首页Banner列表
////        bitmap =
////            ImageResizer.decodeSampledBitmapFromFile(tmpFile.toString(),
////                imageUrl.getReqWidth(),
////                imageUrl.getReqHeight());
//        
//        bitmap=ImageFetcher.downloadBitmapToBitmap(DXApplication.getContext(), imageUrl.getImageUrl(), imageUrl.getReqWidth(), imageUrl.getReqHeight());
//        
//        if (null == bitmap)
//        {
//            return;
//        }
//        
//        //保存文件到磁盘(缓存)
//        cache.addBitmapToCache(imageUrl.getImageUrl(), bitmap);
//    }
    
    @Override
    public boolean equals(Object o)
    {
        if (!(o instanceof PreLoadCacheImageTask))
        {
            return false;
        }
        
        return this.toString().equals(o.toString());
    }
    
    //    equals 返回 true的链各个对象则hashcode必须相等
    @Override
    public int hashCode()
    {
        return toString().hashCode();
    }
    
    //提供Equals对比，单例任务(同一时间只需运行一个)
    @Override
    public String toString()
    {
        return null == imageUrlList || imageUrlList.isEmpty() ? "null" : imageUrlList.toString();
    }
    
    @Override
    protected void executeResponse(ArrayList<ImageDownloadParam> result)
    {
        super.executeResponse(result);
    }
    
}
